//
//  ShareSdkViewController.m
//  JxCarios
//
//  Created by xcq on 15/2/21.
//  Copyright (c) 2015年 xiongchuanqi. All rights reserved.
//

#import "ShareSdkViewController.h"
#import <ShareSDK/ShareSDK.h>


@interface ShareSdkViewController ()<UIGestureRecognizerDelegate>

@end

@implementation ShareSdkViewController

- (void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = YES;
    UIView *btn = (UIView *)[self.tabBarController.view  viewWithTag:400];
    btn.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = NO;
    UIView *btn = (UIView *)[self.tabBarController.view  viewWithTag:400];
    btn.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initAbout];
    [self netRequest];
}

- (void)netRequest {
    if (!_carAround) {
        _carAround = [CarAroundDataSource dataSourceWithRequestor:self];
    }
    [_carAround getShareRule:^(id result) {
        _tableDataSource = [result customObjectForkey:@"share"];
        [self initDataSource];
    } anfFail:^(id result) {
        
    }];
}

- (void)initDataSource {
    if (_tableDataSource.count == 0) {
        return;
    }
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [_tableView reloadData];
    [self initDic];
}

- (void)initDic {
    _dic = [NSMutableDictionary new];
    for (NSDictionary *dic in _tableDataSource) {
        [_dic setObject:[dic customObjectForkey:@"SHARE_VB"] forKey:[dic customObjectForkey:@"SHARE_TYPE"]];
    }
}

- (void)initAbout {
    self.title = @"分享有礼";
    [self showNavBarWithRightTitle:nil andRightAction:nil andLeftAction:nil andCustom:NO];
    _tableView.tableFooterView = [UIView new];
    for (int i = 0 ; i < _shareView.count; i ++) {
        UIView *view = _shareView[i];
        [view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(TapAction:)]];
        
    }
}

- (void)TapAction:(UIGestureRecognizer *)gesture {
    NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"58" ofType:@"png"];
    
    
    
    
    //构造分享内容
    id<ISSContent> publishContent = [ShareSDK content:@"分享内容"
                                       defaultContent:@"免费查违章，五元洗车，找实惠，找周边请上车联网。"
                                                image:[ShareSDK imageWithPath:imagePath]
                                                title:@"车联网"
                                                  url:@"http://api.clejw.com/apk/jxcl.apk"
                                          description:@"免费查违章，五元洗车，找实惠，找周边请上车联网。"
                                            mediaType:SSPublishContentMediaTypeNews];
    
    
    //定制短信信息
    [publishContent addSMSUnitWithContent:@"Hello SMS"];
    
    //定制微信好友信息
    [publishContent addWeixinSessionUnitWithType:INHERIT_VALUE
                                         content:@"免费查违章，五元洗车，找实惠，找周边请上车联网。"
                                           title:@"车联网"
                                             url:@"http://api.clejw.com/apk/jxcl.apk"
                                      thumbImage:[ShareSDK imageWithUrl:@"http://img1.bdstatic.com/img/image/67037d3d539b6003af38f5c4c4f372ac65c1038b63f.jpg"]
                                           image:INHERIT_VALUE
                                    musicFileUrl:nil
                                         extInfo:nil
                                        fileData:nil
                                    emoticonData:nil];
    //定制微信朋友圈信息
    [publishContent addWeixinTimelineUnitWithType:INHERIT_VALUE
                                          content:@"免费查违章，五元洗车，找实惠，找周边请上车联网。"
                                            title:@"车联网"
                                              url:@"http://api.clejw.com/apk/jxcl.apk"
                                       thumbImage:[ShareSDK imageWithUrl:@"http://img1.bdstatic.com/img/image/67037d3d539b6003af38f5c4c4f372ac65c1038b63f.jpg"]
                                            image:INHERIT_VALUE
                                     musicFileUrl:nil
                                          extInfo:nil
                                         fileData:nil
                                     emoticonData:nil];
    
    //定制QQ分享信息
    [publishContent addQQUnitWithType:INHERIT_VALUE
                              content:@"免费查违章，五元洗车，找实惠，找周边请上车联网。"
                                title:@"车联网"
                                  url:@"http://api.clejw.com/apk/jxcl.apk"
                                image:INHERIT_VALUE];
    
    
    
    [ShareSDK showShareActionSheet:nil
                         shareList:nil
                           content:publishContent
                     statusBarTips:YES
                       authOptions:nil
                      shareOptions: nil
                            result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
                                if (state == SSResponseStateSuccess)
                                {
                                    switch (type) {
                                        case  ShareTypeWeixiSession:{
                                            [self shareGetScope:@"weixin_share"];
                                            break;
                                        }
                                        case ShareTypeWeixiTimeline:{
                                            //微信朋友圈
                                            [self shareGetScope:@"friends_share"];

                                            break;
                                        }
                                        case ShareTypeSinaWeibo:{
                                            //分享到微博
                                            [self shareGetScope:@"weibo_share"];

                                            break;
                                        }
                                        case ShareTypeQQSpace: {
//                                            [self shareGetScope:@"weibo_share"];

                                            break;
                                        }
                                        default:
                                            
                                            break;
                                    }
            
                                }
                                else if (state == SSResponseStateFail)
                                {
                                    NSLog(NSLocalizedString(@"TEXT_SHARE_FAI", @"发布失败!error code == %d, error code == %@"), [error errorCode], [error errorDescription]);
                                }
                            }];

    
    
}

- (void)shareGetScope:(NSString *)shareType {
    //微信好友
    _carAround.shareType = shareType;
    if([_dic customObjectForkey:@"shareType"]){
        _carAround.vb = [_dic customObjectForkey:shareType];
    }else{
        return;
    }
    NSString *shareMsg = nil;
    if([Common currenLoginStatus]){ //已登录
        _carAround.shopId = [Common currentUserId];
        shareMsg = [NSString stringWithFormat:@"分享成功,获得%@积分",_carAround.vb];
    }else{
        _carAround.shopId = @"";
        shareMsg = @"分享成功";

    }
    [_carAround getshareScope:^(id result) {
        [self.view makeToast:shareMsg];
    } anfFail:^(id result) {
        
    }];
}


#pragma mark - tableView Delegate & dataSouece
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  _tableDataSource.count > 0 ? _tableDataSource.count : 0;
}
static NSString *cellStr = @"cell";
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellStr];
    }
    NSString *get = @"可获: ";
    cell.textLabel.text = [[_tableDataSource objectAtIndex:indexPath.row] customObjectForkey:@"SHARE_NAME"];
    cell.textLabel.textColor = [UIColor grayColor];
    cell.detailTextLabel.text = [get stringByAppendingString:[[_tableDataSource objectAtIndex:indexPath.row] customObjectForkey:@"SHARE_VB"]];
    cell.detailTextLabel.textColor = [UIColor grayColor];
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 

@end
